﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using 多协议通信平台.Enums;

namespace 多协议通信平台.Views.Protocal.Components
{
    /// <summary>
    /// ModbusSettingHeader.xaml 的交互逻辑
    /// </summary>
    public partial class SerialPortSettingView : UserControl
    {
        public SerialPortSettingView()
        {
            InitializeComponent();
            DicInit();
        }
        private void DicInit()
        {
            string[] ports = SerialPort.GetPortNames();
            foreach (var port in ports)
            {
                PortNames.Add(port);
            }

            foreach (var val in Enum.GetValues<StopBits>())
            {
                string name = Enum.GetName<StopBits>(val);
                StopBitDic.Add(name, val);
            }

            foreach (var val in Enum.GetValues<Parity>())
            {
                string name = Enum.GetName<Parity>(val);
                Parities.Add(name, val);
            }

            CommunicationTypes.Add("串口", "SerialPort");
            CommunicationTypes.Add("TCP/IP", "Network");

            TransmissionModes.Add("RTU");
            TransmissionModes.Add("ASCII");
        }

        public Dictionary<string, string> CommunicationTypes { get; set; } = new Dictionary<string, string>();
        public List<string> TransmissionModes { get; set; } = new List<string>();

        public ObservableCollection<string> PortNames { get; set; } = new ObservableCollection<string>();
        public List<int> BaudRates { get; set; } = new List<int>() { 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 38400, 56000, 57600, 115200 };
        public List<int> DataBits { get; set; } = new List<int>() { 5, 7, 8 };
        public Dictionary<string, Parity> Parities { get; set; } = new Dictionary<string, Parity>();
        public Dictionary<string, StopBits> StopBitDic { get; set; } = new Dictionary<string, StopBits>();


        public ICommand UConnectCommand
        {
            get { return (ICommand)GetValue(UConnectCommandProperty); }
            set { SetValue(UConnectCommandProperty, value); }
        }

        public static readonly DependencyProperty UConnectCommandProperty =
            DependencyProperty.Register("UConnectCommand", typeof(ICommand), typeof(SerialPortSettingView), new PropertyMetadata(null));

        public ICommand UnConnectCommand
        {
            get { return (ICommand)GetValue(UnConnectCommandProperty); }
            set { SetValue(UnConnectCommandProperty, value); }
        }

        public static readonly DependencyProperty UnConnectCommandProperty =
            DependencyProperty.Register("UnConnectCommand", typeof(ICommand), typeof(SerialPortSettingView), new PropertyMetadata(null));

        public object UCommandParameter
        {
            get { return (object)GetValue(UCommandParameterProperty); }
            set { SetValue(UCommandParameterProperty, value); }
        }

        public static readonly DependencyProperty UCommandParameterProperty =
            DependencyProperty.Register("UCommandParameter", typeof(object), typeof(SerialPortSettingView), new PropertyMetadata(null));

        public string CommunicationType
        {
            get { return (string)GetValue(CommunicationTypeProperty); }
            set { SetValue(CommunicationTypeProperty, value); }
        }

        public static readonly DependencyProperty CommunicationTypeProperty =
            DependencyProperty.Register("CommunicationType", typeof(string), typeof(SerialPortSettingView), new PropertyMetadata("SerialPort"));


        public string TransmissionMode
        {
            get { return (string)GetValue(TransmissionModeProperty); }
            set { SetValue(TransmissionModeProperty, value); }
        }

        public static readonly DependencyProperty TransmissionModeProperty =
            DependencyProperty.Register("TransmissionMode", typeof(string), typeof(SerialPortSettingView), new PropertyMetadata("RTU"));


        public string ConnectionStatus
        {
            get { return (string)GetValue(ConnectionStatusProperty); }
            set { SetValue(ConnectionStatusProperty, value); }
        }

        public static readonly DependencyProperty ConnectionStatusProperty =
            DependencyProperty.Register("ConnectionStatus", typeof(string), typeof(SerialPortSettingView), new PropertyMetadata("未连接"));


        public bool Status
        {
            get { return (bool)GetValue(StatusProperty); }
            set { SetValue(StatusProperty, value); }
        }

        public static readonly DependencyProperty StatusProperty =
            DependencyProperty.Register("Status", typeof(bool), typeof(SerialPortSettingView), new PropertyMetadata(false));


        public string IP
        {
            get { return (string)GetValue(IPProperty); }
            set { SetValue(IPProperty, value); }
        }

        public static readonly DependencyProperty IPProperty =
            DependencyProperty.Register("IP", typeof(string), typeof(SerialPortSettingView), new PropertyMetadata(""));


        public int Port
        {
            get { return (int)GetValue(PortProperty); }
            set { SetValue(PortProperty, value); }
        }

        public static readonly DependencyProperty PortProperty =
            DependencyProperty.Register("Port", typeof(int), typeof(SerialPortSettingView), new PropertyMetadata(502));



        public int RecvTimeout
        {
            get { return (int)GetValue(RecvTimeoutProperty); }
            set { SetValue(RecvTimeoutProperty, value); }
        }

        public static readonly DependencyProperty RecvTimeoutProperty =
            DependencyProperty.Register("RecvTimeout", typeof(int), typeof(SerialPortSettingView), new PropertyMetadata(5000));

        public int SlaveId
        {
            get { return (int)GetValue(SlaveIdProperty); }
            set { SetValue(SlaveIdProperty, value); }
        }

        public static readonly DependencyProperty SlaveIdProperty =
            DependencyProperty.Register("SlaveId", typeof(int), typeof(SerialPortSettingView), new PropertyMetadata(1));


        public string ComPort
        {
            get { return (string)GetValue(ComPortProperty); }
            set { SetValue(ComPortProperty, value); }
        }

        public static readonly DependencyProperty ComPortProperty =
            DependencyProperty.Register("ComPort", typeof(string), typeof(SerialPortSettingView), new PropertyMetadata(""));


        public int BaudRate
        {
            get { return (int)GetValue(BaudRateProperty); }
            set { SetValue(BaudRateProperty, value); }
        }

        public static readonly DependencyProperty BaudRateProperty =
            DependencyProperty.Register("BaudRate", typeof(int), typeof(SerialPortSettingView), new PropertyMetadata(9600));


        public int DataBit
        {
            get { return (int)GetValue(DataBitProperty); }
            set { SetValue(DataBitProperty, value); }
        }

        public static readonly DependencyProperty DataBitProperty =
            DependencyProperty.Register("DataBit", typeof(int), typeof(SerialPortSettingView), new PropertyMetadata(8));


        public Parity Parity
        {
            get { return (Parity)GetValue(ParityProperty); }
            set { SetValue(ParityProperty, value); }
        }

        public static readonly DependencyProperty ParityProperty =
            DependencyProperty.Register("Parity", typeof(Parity), typeof(SerialPortSettingView), new PropertyMetadata(Parity.None));


        public StopBits StopBit
        {
            get { return (StopBits)GetValue(StopBitProperty); }
            set { SetValue(StopBitProperty, value); }
        }

        public static readonly DependencyProperty StopBitProperty =
            DependencyProperty.Register("StopBit", typeof(StopBits), typeof(SerialPortSettingView), new PropertyMetadata(StopBits.One));

        private void Button_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            Button? btn = sender as Button;
            btn1.IsEnabled = btn.IsEnabled ? false : true;
        }
    }
}
